<template>
	<div class="edit-popup">
		<popup ref="popupRef" :title="popupTitle" :async="true" width="880px" @confirm="handleSubmit"
			@close="handleClose">
			<div>
				<el-card class="!border-none" shadow="never">
					<el-form class="mb-[-16px]" :model="queryParams" inline>
						<el-form-item label="商品分类" prop="cid">
							<el-select class="w-[200px]" v-model="queryParams.cid">
								<el-option label="全部" value />
								<el-option v-for="item in optionsData.cates" :key="item.id" :label="item.name"
									:value="item.id" />
							</el-select>
						</el-form-item>
						<el-form-item label="标题" prop="title">
							<el-input class="w-[200px]" v-model="queryParams.title" clearable placeholder="请输入标题" />
						</el-form-item>
						<el-form-item>
							<el-button type="primary" @click="resetPage">查询</el-button>
							<el-button @click="resetParams">重置</el-button>
						</el-form-item>
					</el-form>
				</el-card>
				<el-card class="!border-none" v-loading="pager.loading" shadow="never">
					<div class="">
						<el-table :data="pager.lists" style="width: 100%" height="460px" highlight-current-row
							@current-change="handleCurrentChange">
							<el-table-column label="商品分类" prop="cate_name" min-width="80" />
							<el-table-column label="标题" prop="title" min-width="150" show-overflow-tooltip />
							<el-table-column label="图片" min-width="80">
								<template #default="{ row }">
									<image-contain v-if="row.image" :src="row.image" :width="60" :height="45"
										:preview-src-list="[row.image]" preview-teleported fit="contain" />
								</template>
							</el-table-column>
							<el-table-column label="金额" prop="money" min-width="60" />
							<el-table-column label="数量" prop="num" min-width="120">
								<template #default="{ row }">
									<el-input-number style="width: 108px" v-model="row.num" :min="1" :max="999999" />
								</template>
							</el-table-column>
						</el-table>
					</div>
					<div class="flex mt-4 justify-end">
						<pagination v-model="pager" @change="getLists" />
					</div>
				</el-card>
			</div>
		</popup>
	</div>
</template>

<script lang="ts" setup name="goodsEdit">
	import Popup from '@/components/popup/index.vue'
	import type { PropType } from 'vue'
	import { usePaging } from '@/hooks/usePaging'
	import { useDictOptions } from '@/hooks/useDictOptions'
	import { apiGoodsLists } from '@/api/goods'
	import { apiGoodsCateAll } from '@/api/goods_cate'
	import { dispatch } from '@/api/goods_record'
	import feedback from '@/utils/feedback'
	import { isEmpty } from 'lodash-es'

	defineProps({
		dictData: {
			type: Object as PropType<Record<string, any[]>>,
			default: () => ({})
		}
	})
	const emit = defineEmits(['success', 'close'])
	const popupRef = shallowRef<InstanceType<typeof Popup>>()


	// 弹窗标题
	const popupTitle = computed(() => {
		return '商品列表'
	})

	const { optionsData } = useDictOptions<{
		cates : any[]
	}>({
		cates: {
			api: apiGoodsCateAll
		}
	})

	// 查询条件
	const queryParams = reactive({
		cid: '',
		title: '',
		is_show: 1
	})

	// 表单数据
	const formData = reactive({
		id: '',
		goods_id: '',
		num: 1
	})

	// 选中数据
	const currentRow = ref()

	// 表格选择后回调事件
	const handleCurrentChange = (val : string) => {
		currentRow.value = val
	}

	// 分页相关
	const { pager, getLists, resetParams, resetPage } = usePaging({
		fetchFun: apiGoodsLists,
		params: queryParams
	})


	// 提交按钮
	const handleSubmit = async () => {
		if (isEmpty(currentRow.value)) return feedback.msgError('请选择商品')
		formData.goods_id = currentRow.value.id;
		formData.num = currentRow.value.num;
		await dispatch(formData)
		popupRef.value?.close()
		emit('success')
	}

	//打开弹窗
	const open = () => {
		popupRef.value?.open()
	}

	// 关闭回调
	const handleClose = () => {
		emit('close')
	}

	const setFormData = async (data : Record<any, any>) => {
		formData.id = data.id;
	}

	defineExpose({
		open,
		setFormData,
	})
	getLists()
</script>